package org.zjx.action.game;

import org.zjx.action.Action;
import org.zjx.action.basic.DelayAction;
import org.zjx.action.basic.HotkeyAction;
import org.zjx.action.basic.ScreenshotAction;
import org.zjx.core.IWindow;

import java.awt.*;

public abstract class AbstractWindowAction extends Action {

    public AbstractWindowAction(IWindow targetWindow) {
        super(targetWindow, false);
    }

    public AbstractWindowAction(IWindow targetWindow, boolean requiresActiveWindow) {
        super(targetWindow, requiresActiveWindow);
    }

    public abstract void open();

    public abstract void close();

    public abstract void reSetWindow();

    public abstract void mouseAloofWindow(Rectangle rect);

    public void flush() {
        new ScreenshotAction(targetWindow).execute();
    }

    public void hotkey(int... keys) {
        new HotkeyAction(targetWindow, keys).execute();
        new DelayAction(targetWindow, 500).execute();
    }

    public void click(Rectangle rectangle) {
        targetWindow.execute(op -> {
            op.gameMove(rectangle);
            op.delay(30);
            op.click(1);
            op.delay(30);
            return null;
        });
    }

    public void click(double x, double y) {
        click((int) x, (int) y);
    }

    public void click(int x, int y) {
        targetWindow.execute(op -> {
            op.click(x, y);
            op.delay(100);
            return null;
        });
    }

    public void move(Rectangle rectangle) {
        move(rectangle.getCenterX(), rectangle.getCenterY());
    }

    public void move(Point point) {
        move(point.x, point.y);
    }

    public void move(double x, double y) {
        move((int) x, (int) y);
    }

    public void move(int x, int y) {
        targetWindow.getState().getGameState().setGameMousePoint(new  Point(x, y));
        targetWindow.execute(op -> {
            op.move(x, y);
            return null;
        });
    }

    public void drag(Rectangle from, Rectangle to) {
        drag(from.x, from.y, to.x, to.y);
    }

    public void drag(double fromX, double fromY, double toX, double toY) {
        drag((int) fromX, (int) fromY, (int) toX, (int) toY);
    }

    public void drag(int fromX, int fromY, int toX, int toY) {
        targetWindow.execute(op -> {
            op.drag(fromX, fromY, toX, toY);
            op.delay(100);
            return null;
        });
    }

    public void delay(int milliseconds) {
        new DelayAction(targetWindow, milliseconds).execute();
    }
}
